

import 'package:flutter/material.dart' hide Action;
import 'package:flutter_dva/flutter_dva.dart';
import 'package:flutter_ui/app.dart';

class NavModel implements Model<StateTmpl> {
  @override
  Map<String, EffectFunction> effects = {
    "navPage" : (Action action, ModelEffects modelEffects) async {
      Map<String,dynamic> payload = action.payload != null ? action.payload : Map<String,dynamic>();
      BuildContext context =  payload.containsKey("context") ? payload["context"] : MyApp.rootContext;
      String routeName =  payload["routeName"];
      Object arguments = payload["arguments"];

      return await Navigator.pushNamed(context, routeName, arguments: arguments);
    },
    "goBack": (Action action, ModelEffects modelEffects) async {
      Map<String,dynamic> payload = action.payload != null ? action.payload : Map<String,dynamic>();
      BuildContext context =  payload.containsKey("context") ? payload["context"] : MyApp.rootContext;
      dynamic result = payload["result"];

      return Navigator.maybePop(context, result);
    },
    "resetPage": (Action action, ModelEffects modelEffects) async {
      Map<String,dynamic> payload = action.payload != null ? action.payload : Map<String,dynamic>();
      BuildContext context =  payload.containsKey("context") ? payload["context"] : MyApp.rootContext;
      String routeName =  payload["routeName"];
      Object arguments = payload["arguments"];

      return await Navigator.pushNamedAndRemoveUntil(context, routeName, ModalRoute.withName('/'), arguments: arguments);
    },
  };

  @override
  String namespace = "nav";

  @override
  Map<String, ReducerFuction> reducers = <String, ReducerFuction>{};

  @override
  StoreOfState<StateTmpl> state = new StoreOfState<StateTmpl>(state: StateTmpl());
}